package com.dengqi.mymodule.activity.softinput;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;


public class SoftInputActivity extends BaseActivity {

    private EditText et1, et2, et3, et4;
    private RelativeLayout layout_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_softinput);

        et1 = findViewById(R.id.editText1);
        et2 = findViewById(R.id.editText2);
        et3 = findViewById(R.id.editText3);
        et4 = findViewById(R.id.editText4);

        et1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.e("---", "et1:" + hasFocus);
            }
        });
        et2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.e("---", "et2:" + hasFocus);
            }
        });
        et3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.e("---", "et3:" + hasFocus);
            }
        });
        et4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.e("---", "et4:" + hasFocus);
            }
        });
        //方法一：最外层布局添加点击事件
        layout_main = findViewById(R.id.layout_main);
        layout_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("---", "main");
                closeKeyboard();
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        int[] leftTop = {0, 0};
        // 获取输入框当前的location位置
        et4.getLocationInWindow(leftTop);
        int left = leftTop[0];
        int top = leftTop[1];
        int bottom = top + et4.getHeight();
        int right = left + et4.getWidth();
        Log.e("---", left+" - "+right+" - "+top+" - "+bottom);
    }

    //方法二：有bug：软键盘弹出后view.getLocationInWindow(leftTop);方法获取到的edittext控件位置存在偏差
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent event) {
//        if (event.getAction() == MotionEvent.ACTION_DOWN) {
//            // 获取当前获得当前焦点所在View
//            Log.e("---event", event.getX() + " - " + event.getY());
//            View view = getCurrentFocus();
//            if (view != null) {
//                String tag = view.getTag().toString();
//                Log.e("---dis", tag);
//            }
//            if (!isClickEt(view, event)) {
//                // 如果不是edittext，则隐藏键盘
//                if (closeKeyboard()) {
//                    view.clearFocus();
//                    return true;
//                }
//            }
//            return super.dispatchTouchEvent(event);
//        }
//        // 必不可少，否则所有的组件都不会有TouchEvent了
//        return getWindow().superDispatchTouchEvent(event) || onTouchEvent(event);
//    }
//
//
//    /**
//     * 获取当前点击位置是否为et
//     *
//     * @param view  焦点所在View
//     * @param event 触摸事件
//     */
//    public boolean isClickEt(View view, MotionEvent event) {
//        if (view != null && (view instanceof EditText)) {
//            int[] leftTop = {0, 0};
//            // 获取输入框当前的location位置
//            // 获取的是软键盘弹出后et的位置 有bug
//            view.getLocationInWindow(leftTop);
//            int left = leftTop[0];
//            int top = leftTop[1];
//            // 此处根据输入框左上位置和宽高获得右下位置
//            int bottom = top + view.getHeight();
//            int right = left + view.getWidth();
//            Log.e("---dis1", view.getTag().toString());
//            Log.e("---dis2", event.getX() + " - " + event.getY());
//            Log.e("---dis3", left + " - " + right + " - " + top + " - " + bottom);
//            boolean bool = event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom;
//            Log.e("---dis4", bool + "");
//            if (bool) {
//                // 点击的是输入框区域，保留点击EditText的事件
//                return true;
//            } else {
//                return false;
//            }
//        }
//        return false;
//    }

    /**
     * 弹出软键盘
     */
    private boolean showKeyboard(EditText editText) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        return inputMethodManager.showSoftInput(editText, 0);
    }

    /**
     * 收回软键盘
     */
    private boolean closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            return inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
        return false;
    }
}
